package com.jarich.morewore;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.jarich.morewore.model.Plaats;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Connector {

	/** Api address */
	private static final String ADDRESS = "http://romaap.com/api.php";

	/**
	 * Makes a connection with the server and posts the given JSONObject.
	 * 
	 * @param JSONObject
	 *            the JSON object to be posted to the server
	 * @return String a string containing the servers response
	 */
	public static String sendRequest(JSONObject object) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(ADDRESS);
		String request = "";
		try {
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
			nameValuePairs.add(new BasicNameValuePair("APIKey", "ultraQuote"));
			nameValuePairs.add(new BasicNameValuePair("payload", object
					.toString()));
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			request = EntityUtils.toString(entity);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException i) {
			i.printStackTrace();
		}
		return request;
	}


	public static ArrayList<Plaats> getTheCityList(double lat1, double lat2, double lon1, double lon2) {
		JSONObject object = new JSONObject();
		try {
			object.put("Action", "GetCities");
			object.put("Lat1", lat1);
			object.put("Lat2", lat2);
			object.put("Long1", lon1);
			object.put("Long2", lon2);
		} catch (JSONException e) {
			System.out.println("Error in the method: getTheItemList()");
		}
		return getCityList(object);
	}

	
	public static ArrayList<Plaats> getCityList(JSONObject object) {
		ArrayList<Plaats> plaatsen = new ArrayList<Plaats>();
		try {
			JSONObject object1 = new JSONObject(sendRequest(object));
			//System.out.println(object1);
			// Fill an array with the received coupons
			JSONArray array = object1.getJSONArray("Cities");
			int amountOfItems = 0;
			// As long as there are element in the Array, continue.
			while (array.length() > amountOfItems) {
				// Pick the next coupon in the Array.
				JSONObject q1 = array.getJSONObject(amountOfItems);

				// Store the coupon info.

				String naam = q1.getString("City");
				String plaatsNaam = q1.getString("AccentCity");
				int inwoners = q1.getInt("Population");
				double lat = q1.getDouble("Latitude");
				double lon = q1.getDouble("Longitude");
				amountOfItems++;
				plaatsen.add(new Plaats(naam, plaatsNaam, inwoners, lat, lon));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return plaatsen;
	}
	
	public static ArrayList<String> getCitiesFiltered(String filter){
		JSONObject object = new JSONObject();
		try {
			object.put("Action", "GetCitiesFiltered");
			object.put("filter", filter);
		} catch (JSONException e) {
			System.out.println("Error in the method: getTheItemList()");
		}
		return getFilteredCityList(object);
	}
	
	public static ArrayList<String> getFilteredCityList(JSONObject object) {
		ArrayList<String> plaatsen = new ArrayList<String>();
		try {
			JSONObject object1 = new JSONObject(sendRequest(object));
			JSONArray array = object1.getJSONArray("Cities");
			int amountOfItems = 0;
			while (array.length() > amountOfItems) {
				JSONObject q1 = array.getJSONObject(amountOfItems);
				
				String plaatsNaam = q1.getString("Name");
				amountOfItems++;
				plaatsen.add(plaatsNaam);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return plaatsen;
	}
}